'''
数据库相关的操作
'''
import pymysql


def connect(db_info):
    # {"host": "192.168.1.7", "port": 3306, "user": "root", "pwd": "", "dbname": "ranzhi"}
    conn = pymysql.connect(
        user=db_info['user'],
        password=db_info['pwd'],
        host=db_info['host'],
        database=db_info['dbname'],
        port=db_info['port'],
        charset="utf8")
    print(f"连接数据库{db_info['host']}：{db_info['port']}成功")
    return conn


def disconnect(conn):
    conn.close()


def execute(conn, sql):
    c = conn.cursor()
    c.execute(sql)  # 执行sql语句
    conn.commit()  # 提交
    r = c.fetchall()  # 获取结果
    print(f"执行sql语句：{sql}")
    return r

#根据手机号码清理用户
def delete_user(db_info,phone):
    c = connect(db_info)
    execute(c,f"delete from member where mobilephone = {phone}")
    disconnect(c)

#根据手机号查询余额
def get_amount(db_info,phone):
    c = connect(db_info)
    r = execute(c,f"select leaveamount from member where mobilephone = {phone}")
    print(f"当前用户{phone}余额为{r}")
    disconnect(c)
    return r

if __name__ == '__main__':
    a = connect({"host": "127.0.0.1", "port":3306, "user": "root", "pwd": "", "dbname": "future"})